package com.education.service.edu.controller.web;

import com.education.dto.CourseDto;
import com.education.result.R;
import com.education.service.edu.entity.Course;
import com.education.service.edu.entity.WebCourseQuery;
import com.education.service.edu.entity.vo.ChapterVo;
import com.education.service.edu.entity.vo.WebCourseVo;
import com.education.service.edu.service.ChapterService;
import com.education.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户访问 - 课程列表 - 前端控制器
 * Created by Lance on 2020/07/01 19:47
 */
@Api(tags = "课程管理")
@Slf4j
@RestController
@RequestMapping("/web/edu/course")
public class WebCourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @ApiOperation("课程列表")
    @GetMapping("list")
    public R list(@ApiParam(value = "查询对象", required = false) WebCourseQuery webCourseQuery){
        List<Course> courseList = courseService.webSelectList(webCourseQuery);
        return  R.ok().data("courseList", courseList);
    }

    @ApiOperation("根据ID获取课程信息")
    @GetMapping("get/{courseId}")
    public R getById(@ApiParam(value = "课程ID", required = true)  @PathVariable String courseId){
        WebCourseVo webCourseVo = courseService.selectWebCourseById(courseId);
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("course", webCourseVo).data("chapterVoList", chapterVoList);
    }

    @ApiOperation("根据课程ID查询课程信息")
    @GetMapping("get-course-dto/{courseId}")
    public R getCourseDtoById(@ApiParam(value = "课程ID", required = true) @PathVariable String courseId){
        CourseDto courseDto = courseService.getCourseDtoById(courseId);
        return R.ok().data("courseDto", courseDto);
    }

    @ApiOperation("根据课程id更改销售量")
    @GetMapping("update-buy-count/{id}")
    public R updateBuyCountById(@ApiParam(value = "课程id", required = true) @PathVariable String id){
        courseService.updateBuyCountById(id);
        return R.ok();
    }
}
